// BreakPoint.cpp: implementation of the CBreakPoint class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "8085.h"
#include "BreakPoint.h"
#include <assert.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBreakPoint::CBreakPoint()
{
    Init();
}

CBreakPoint::~CBreakPoint()
{

}

void CBreakPoint::Init()
{
	DeleteAllBkPt( );
    m_pos = NULL;
}

void CBreakPoint::AddBreakPoint(word address,byte data,int nSrcLine, BOOL initial_state )
{
    OneBreakPoint pt;

    pt.address=address;
    pt.data=data;
    pt.bEnabled = initial_state ;
    pt.m_nSrcLine = nSrcLine;
    m_cBkPt.AddTail(pt);
}

// search for the address in the list of break points and 
// delete it.
// return 0 if unsuccessful
int CBreakPoint::DeleteBkPt(word address)
{
    POSITION pos;
    if(!(pos=Search(address))) return 0;
    m_cBkPt.RemoveAt(pos);
    m_pos = m_cBkPt.GetHeadPosition();
    return 1;
}

// the 'nth' element in the breakpoint array will be deleted
int CBreakPoint::DeleteBkPtAt(int nLocation)
{
    assert(0);
    return 0;
}

int CBreakPoint::IsABkPt(word address)
{
    if(!Search(address)) return 0;
    return 1;
}

int CBreakPoint::GetDataAtBkPt(word address)
{
    POSITION pos;
    if(!(pos=Search(address))) return -1;
    return m_cBkPt.GetAt(pos).data;
}

void CBreakPoint::DeleteAll()
{
    m_cBkPt.RemoveAll();
}

void CBreakPoint::EnableBkPt(word addr, BOOL bEnable)
{
    POSITION pos;
    if(!(pos=Search(addr))) return;
    m_cBkPt.GetAt(pos).bEnabled = bEnable;
}

POSITION CBreakPoint::Search(word addr)
{
    OneBreakPoint pt;
    POSITION pos;
    for( pos=m_cBkPt.GetHeadPosition(); pos ; m_cBkPt.GetNext(pos))
    {
        pt = m_cBkPt.GetAt(pos);
        if(pt.address==addr)
            return pos;
    }
    return NULL;
}

BOOL CBreakPoint::IsBkPtEnabled(word addr)
{
    POSITION pos;
    if(!(pos=Search(addr))) return FALSE;
    return m_cBkPt.GetAt(pos).bEnabled;
}

OneBreakPoint* CBreakPoint::FirstBreakPoint()
{
    m_pos = m_cBkPt.GetHeadPosition();
    if(m_pos==NULL)
        return NULL;
    return &m_cBkPt.GetAt(m_pos);
}

OneBreakPoint* CBreakPoint::NextBreakPoint()
{
    if(m_pos==NULL) 
        return NULL;
    m_cBkPt.GetNext(m_pos);
    if(m_pos==NULL)
        return NULL;
    return &m_cBkPt.GetAt(m_pos);
}

unsigned CBreakPoint::GetCount()
{
    return m_cBkPt.GetCount();
}

void CBreakPoint::DeleteAllBkPt()
{
    m_cBkPt.RemoveAll();
}
